import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import store from '../store'
import { Toast } from 'vant';
import {
	isLogin,
	changeDocumentTitle
} from '../utils'

// 解决重复点击路由报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const router = new VueRouter({
	routes
})

router.beforeEach((to, from, next) => {
	// console.log(to)
	const allowedPageName = ["login","secretPolicy","aboutUs"] //不登录可以访问页面
	const isLoginResult = isLogin()
	if(!isLoginResult&&!allowedPageName.includes(to.name)){
		Toast('请登录');
		next("/login")
		return
	}
	// 更改浏览器title
	changeDocumentTitle(to)
	// 切换tabbar样式
	store.commit("home/changeTabActive",to.meta.tab)
	next()
})

export default router